//window.onload = function() {
//	// alert('window'); 进入界面就加载该函数
//	// init();
//	// location.reload();
//}

var type=1;	// 1:用户表展示，2：任务表展示

$("#menudiv a").on("click",function(){
	var text =$(this).text();
	var mapChart = echarts.init(document.getElementById('jobdata_chart'));
	mapChart.dispose();
	if(text=="用户查看"){
		type=1;
		initTable1();
		var target=document.getElementById('taskdesc');
		target.style.display = 'none';
		
		var target2=document.getElementById('userdesc');
		target2.style.display = 'none';
		var target3=document.getElementById('chartshow');
		target3.style.display = 'none';
		
		var target4=document.getElementById('boottab');
		target4.style.display = 'block';
	}else if(text =='用户说明'){
		type=1;
		initTable1();
		var target=document.getElementById('userdesc');
		target.style.display = 'block';
		var target2=document.getElementById('taskdesc');
		target2.style.display = 'none';
		var target3=document.getElementById('chartshow');
		target3.style.display = 'none';
		var target4=document.getElementById('boottab');
		target4.style.display = 'block';
	}else if(text =='任务查看'){
		type=2;
		initTable2();
		var target=document.getElementById('taskdesc');
		target.style.display = 'none';
		var target2=document.getElementById('userdesc');
		target2.style.display = 'none';
		var target3=document.getElementById('chartshow');
		target3.style.display = 'none';
		var target4=document.getElementById('boottab');
		target4.style.display = 'block';
	}else if(text =='任务资料'){
		type=2;
		initTable2();
		var target=document.getElementById('taskdesc');
		target.style.display = 'block';
		var target2=document.getElementById('userdesc');
		target2.style.display = 'none';
		var target3=document.getElementById('chartshow');
		target3.style.display = 'none';
		var target4=document.getElementById('boottab');
		target4.style.display = 'block';
	}else if(text=='日爬取量统计'){
		count();
		var target=document.getElementById('taskdesc');
		target.style.display = 'none';
		var target2=document.getElementById('userdesc');
		target2.style.display = 'none';
		var target4=document.getElementById('boottab');
		target4.style.display = 'none';
		var target3=document.getElementById('chartshow');
		target3.style.display = 'block';
	}
});

$("#loginExit").click(function(){
	$.ajax({
		type : "POST", // 请求方式
		url : "../jobsys/user/exit", // 发送请求地址
		dataType : "json",
		async : false,
		traditional : true,
		data : {
		},
		beforeSend : function() {
			
		},
		// 请求成功后的回调函数有两个参数
		success : function(re) {
			// var json = eval("(" + data + ")");// 将json类型字符串转换为json对象
			var result = eval(re);
			alert(result.msg);
			if(result.success){
				top.window.location = '../jobsys/user';
			}				
		},
		completed : function() {

		},
		error : function(re) {
			// 请求出错处理 
			//alert("服务器错误");
		}
	});
});

////////////////////////////////////////////////////////////////
function initTable1() {  
    //先销毁表格  
    $('#bootstrp1').bootstrapTable('destroy');  
    //初始化表格,动态从服务器加载数据  
    $("#bootstrp1").bootstrapTable({  
    	toolbar: "#toolbar",
    	idField: "Id",
        clickToSelect: false,
        method: "get",  //使用get请求到服务器获取数据  
        url: "../jobsys/manager/queryAllUser", //获取数据的Servlet地址  
        striped: true,  //表格显示条纹  
        pagination: true, //启动分页  
        pageSize: 10,  //每页显示的记录数  
        pageNumber:1, //当前第几页  
        pageList: [5, 10, 15, 20, 25],  //记录数可选列表  
        search: false,  //是否启用查询  
        showColumns: true,  //显示下拉框勾选要显示的列  
        showRefresh: true,  //显示刷新按钮  
        sidePagination: "server", //表示服务端请求  
        //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder  
        //设置为limit可以获取limit, offset, search, sort, order  
        queryParamsType : "undefined",   
        queryParams: function queryParams(params) {   //设置查询参数  
          var param = {    
              pageNumber: params.pageNumber,    
              pageSize: params.pageSize,  
              orderNum : $("#orderNum").val()  
          };    
          return param;                   
        },
        columns: [{
            checkbox: true
        }, {
            field: "id",
            title: "id"
        }, {
            field: "userid",
            title: "用户ID",
            formatter: function (value, row, index) {
                return "<a href=\"#\" name=\"userID\" class=\"editable editable-click\" data-type=\"text\" data-pk=\""+row.userid+"\" data-title=\"用户ID\">" + value + "</a>";
            },
            editable: {
                type: 'text',
                title: '用户ID',
                disabled: false,             //是否禁用编辑
                emptytext: "空文本",          //空值的默认文本
                mode: "inline",   
                validate: function (v) {
                    if (!$.trim(v)) return '用户名不能为空';

                }
            }
        }, {
            field: "nickname",
            title: "昵称",
            formatter: function (value, row, index) {
                return "<a href=\"#\" name=\"nickname\" data-type=\"text\" data-pk=\""+row.nickname+"\" data-title=\"昵称\">" + value + "</a>";
            }
        }, {
            field: "pwd",
            title: "用户密码",
            formatter: function (value, row, index) {
                return "<a href=\"#\" name=\"pwd\" data-type=\"text\" data-pk=\""+row.pwd+"\" data-title=\"用户密码\">" + value + "</a>";
            }
        }, {
            field: "rightx",
            title: "用户权限",
            formatter: function (value, row, index) {
                return "<a href=\"#\" name=\"rightx\" data-type=\"text\" data-pk=\""+row.right+"\" data-title=\"用户权限\">" + value + "</a>";
            }
        }, {
            field: "isdelete",
            title: "删除状态",
            formatter: function (value, row, index) {
                return "<a href=\"#\" name=\"isdelete\" data-type=\"text\" data-pk=\""+row.isdelete+"\" data-title=\"删除状态\">" + value + "</a>";
            }
        }],
        onEditableSave: function (field, row, oldValue, $el) {
            $.ajax({
                type: "post",
                url: "/Editable/Edit",
                data: row,
                dataType: 'JSON',
                success: function (data, status) {
                    if (status == "success") {
                        alert('提交数据成功');
                    }
                },
                error: function () {
                    alert('编辑失败');
                },
                complete: function () {

                }

            });
        },
        onClickRow: function (row, $element) {
            curRow = row;
        },
        onLoadSuccess: function(){  //加载成功时执行  
          //alert("加载成功");
        	 $("#bootstrp1 a").editable({
                 url: function (params) {
                     var sName = $(this).attr("name");
                     curRow[sName] = params.value;
                     $.ajax({
                         type: 'post',
                         url: "../jobsys/manager/updateUserinfo",
                         data: curRow,
                         dataType: 'json',
                         async : false,
                 		traditional : true,
                         success: function (data, textStatus, jqXHR) {
                        	 alert(data.msg);
                         },
                         error: function () { alert("error");}
                     });
                 },
                 type: 'text'
             });
        },  
        onLoadError: function(){  //加载失败时执行  
          alert("加载数据失败", {time : 1500, icon : 2});  
          top.window.location = '../jobsys/job';
        }  
      });  
  }

function initTable2() {  
    //先销毁表格  
    $('#bootstrp1').bootstrapTable('destroy');  
    //初始化表格,动态从服务器加载数据  
    $("#bootstrp1").bootstrapTable({  
    	toolbar: "#toolbar",
    	idField: "Id",
        clickToSelect: false,
        method: "get",  //使用get请求到服务器获取数据  
        url: "../jobsys/manager/queryAllTask", //获取数据的Servlet地址  
        striped: true,  //表格显示条纹  
        pagination: true, //启动分页  
        pageSize: 10,  //每页显示的记录数  
        pageNumber:1, //当前第几页  
        pageList: [5, 10, 15, 20, 25],  //记录数可选列表  
        search: false,  //是否启用查询  
        showColumns: true,  //显示下拉框勾选要显示的列  
        showRefresh: true,  //显示刷新按钮  
        sidePagination: "server", //表示服务端请求  
        //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder  
        //设置为limit可以获取limit, offset, search, sort, order  
        queryParamsType : "undefined",   
        queryParams: function queryParams(params) {   //设置查询参数  
          var param = {    
              pageNumber: params.pageNumber,    
              pageSize: params.pageSize,  
              orderNum : $("#orderNum").val()  
          };    
          return param;                   
        },
        columns: [{
            checkbox: true
        }, {
            field: "id",
            title: "id"
        }, {
            field: "state",
            title: "运行状态",
            formatter: function (value, row, index) {
                return "<a href=\"#\" name=\"state\" class=\"editable editable-click\" data-type=\"text\" data-pk=\""+row.state+"\" data-title=\"运行状态\">" + value + "</a>";
            },
            editable: {
                type: 'text',
                title: '运行状态',
                disabled: false,             //是否禁用编辑
                emptytext: "空文本",          //空值的默认文本
                mode: "inline",   
                validate: function (v) {
                    if (!$.trim(v)) return '用户名不能为空';

                }
            }
        }, {
            field: "name",
            title: "任务名称",
            formatter: function (value, row, index) {
                return "<a href=\"#\" name=\"name\" data-type=\"text\" data-pk=\""+row.name+"\" data-title=\"任务名称\">" + value + "</a>";
            }
        }, {
            field: "exp",
            title: "表达式",
            formatter: function (value, row, index) {
                return "<a href=\"#\" name=\"exp\" data-type=\"text\" data-pk=\""+row.exp+"\" data-title=\"表达式\">" + value + "</a>";
            }
        }, {
            field: "clazz",
            title: "实现类",
//            formatter: function (value, row, index) {
//                return "<a name=\"clazz\" data-type=\"text\" data-pk=\""+row.clazz+"\" data-title=\"实现类\">" + value + "</a>";
//            },
            editable: {
                type: 'text',
                title: '实现类',
                disabled: true,             //是否禁用编辑
                emptytext: "空文本",          //空值的默认文本
                mode: "inline",   
                validate: function (v) {
                    if (!$.trim(v)) return '实现类不能为空';

                }
            }
        }, {
            field: "info",
            title: "备注",
            formatter: function (value, row, index) {    	
                return "<a href=\"#\" name=\"info\" data-type=\"text\" data-pk=\""+row.info+"\" data-title=\"备注\">" + value + "</a>";
            }
        }],
        onEditableSave: function (field, row, oldValue, $el) {
            $.ajax({
                type: "post",
                url: "/Editable/Edit",
                data: row,
                dataType: 'JSON',
                success: function (data, status) {
                    if (status == "success") {
                        alert('提交数据成功');
                    }
                },
                error: function () {
                    alert('编辑失败');
                },
                complete: function () {

                }

            });
        },
        onClickRow: function (row, $element) {
            curRow = row;
        },
        onLoadSuccess: function(){  //加载成功时执行  
          //alert("加载成功");
        	 $("#bootstrp1 a").editable({
                 url: function (params) {
                     var sName = $(this).attr("name");
                     curRow[sName] = params.value;
                     $.ajax({
                         type: 'post',
                         url: "../jobsys/manager/updateTask",
                         data: curRow,
                         dataType: 'json',
                         async : false,
                 		traditional : true,
                         success: function (data, textStatus, jqXHR) {
                             alert(data.msg);
                         },
                         error: function () { alert("error");}
                     });
                 },
                 type: 'text'
             });
        },  
        onLoadError: function(){  //加载失败时执行  
          alert("加载数据失败", {time : 1500, icon : 2});
          top.window.location = '../jobsys/job';
        }  
      });  
  }

var init = function(){
	$.ajax({
		type: 'post',
        url: "../jobsys/manager/init",
        data: {},
        dataType: 'json',
        async : false,
		traditional : true,
        success: function (data, textStatus, jqXHR) {
        	$("#nickName").text(data.userID);
        },
        error: function () { 
        	alert("error");
        	top.window.location = '../jobsys/job';
        }
	});
}

//$('#example1').Tabledit({
//	url: '../jobsys/user/queryAllUser',
//    columns: {
//        identifier: [0, 'id'],
//        editable: [[1, 'userID','{}'],[2, 'nickname'],[3, 'pwd'],[4,'right'],[5,'isDelete']]
//    },
//    onDraw: function() { return; },
//    onAlways: function() {
//        console.log('onAlways()');
//    },
//    onSuccess:function(re){
//
//    },
//    onAjax: function(action, serialize) {
//        console.log('onAjax(action, serialize)');
//        console.log(action);
//        console.log(serialize);
//        if(action=='edit'){
//        	$(this)[0].url="../jobsys/user/updateUserinfo";
//        }else if(action=='delete'){
//        	$(this)[0].url="../jobsys/user/deleteUser";
//        }else if(action=='save'){
//        	$(this)[0].url="../jobsys/user/update";
//        }else if(action=='restore'){
//        	$(this)[0].url="../jobsys/user/update";
//        }else if(action=='confirm'){
//        	$(this)[0].url="../jobsys/user/deleteUser";
//        }
//    }
//});

$(document).ready(function() {
//	var source = $("input[name='source']:checked").val();
//	$("input[name='source']").eq(source).attr("checked", "checked");
//	var scity = document.getElementById("scity").innerText();		
//	var sindustry = document.getElementById("sindustry").innerText();    
//	var stime = document.getElementById("stime").innerText();	   
//	var ssource = document.getElementById("ssource").innerText();
//	var curpage = $("#curpage").innerText();
//	var queryStr = $("#hd-kw").val();
//	
	//queryAllUser();
	//$('#example1').load();
	if(type==1){
		initTable1();	
	}else{
		initTable2();
	}
	
	init();
});